<template>
	<view class="custom_workbench">
		<map id="map" :longitude="longitude" :latitude="latitude" :style="contentStyle" scale="15" :enable-traffic="false" :show-location="true" :enable-poi="true" class="map">
			<cover-image class="custom_location" src="../../static/workbench/location.png" @tap="returnLocationHandle()"></cover-image>
		</map>
		<view class="panel">
			<view class="from" @tap="chooseLocationHandle('from')">
				<text>{{ from.address }}</text>
			</view>
			<view class="dashed-line"></view>
			<view class="to" @tap="chooseLocationHandle('to')">
				<text>{{ to.address }}</text>
			</view>
		</view>
	</view>
</template>

<script>
const chooseLocation = requirePlugin('chooseLocation');
let QQMapWX = require('../../lib/qqmap-wx-jssdk.min.js');

export default {
	data() {
		return {
			qqmapsdk: null,
			from: {
				address: '',
				longitude: 0,
				latitude: 0
			},
			to: {
				address: '输入你的目的地',
				longitude: 0,
				latitude: 0
			},
			longitude: 116.397505,
			latitude: 39.908675,
			contentStyle: '',
			windowHeight: 0,
			map: null,
			flag: null
		};
	},

	methods: {
		returnLocationHandle: function() {
			this.map.moveToLocation();
		},

		//初始化当前位置
		initLocation() {
			let that = this;
			//获取当前位置信息
			uni.getLocation({
				type: 'gcj02',
				// type: 'wgs84',
				success: function(res) {
					// console.log('定位的位置是:', res)
					that.latitude = res.latitude;
					that.longitude = res.longitude;
					that.from.latitude = res.latitude;
					that.from.longitude = res.longitude;

					that.qqmapsdk.reverseGeocoder({
						location: {
							latitude: that.latitude,
							longitude: that.longitude
						},
						success: function(resp) {
							//console.log(resp);
							that.from.address = resp.result.address;
						},
						fail: function(error) {
							console.log(error);
						}
					});
				}
			});
		},

		chooseLocationHandle: function(flag) {
			let that = this;
			that.flag = flag;
			let key = that.tencent.map.key; //使用在腾讯位置服务申请的key
			// console.log("调用的key值是:", key);
			let referer = that.tencent.map.referer; //调用插件的app的名称
			let latitude = that.latitude;
			let longitude = that.longitude;
			let data = JSON.stringify({
				latitude: latitude,
				longitude: longitude
			});
			uni.navigateTo({
				url: `plugin://chooseLocation/index?key=${key}&referer=${referer}&location=${data}`
			});
		},

		//查询用户订单状态
		queryCustomOrderState(location) {
			let that = this;
			that.ajax(
				that.url.hasCustomerCurrentOrder,
				'POST',
				{},
				resp => {
					let result = resp.data.result;
					let hasCustomerUnAcceptOrder = result.hasCustomerUnAcceptOrder;
					let hasCustomerUnFinishedOrder = result.hasCustomerUnFinishedOrder;
					let orderInfo = result.orderInfo;
					if (hasCustomerUnAcceptOrder) {
						let json = result.unAcceptOrder;
						let carType = json.carType;
						let carPlate = json.carPlate;
						let startPlaceLocation = JSON.parse(json.startPlaceLocation);
						let endPlaceLocation = JSON.parse(json.endPlaceLocation);
						let from = {
							address: json.startPlace,
							latitude: startPlaceLocation.latitude,
							longitude: startPlaceLocation.longitude
						};
						let to = {
							address: json.endPlace,
							latitude: endPlaceLocation.latitude,
							longitude: endPlaceLocation.longitude
						};
						uni.setStorageSync('from', from);
						uni.setStorageSync('to', to);
						uni.showModal({
							title: '提示消息',
							content: '您有一个订单等待司机接单，现在将跳转到等待接单页面',
							showCancel: false,
							success: function(resp) {
								if (resp.confirm) {
									uni.navigateTo({
										url: `../create_order/create_order?showPopup=true&orderId=${json.id}&showCar=true&carType=${carType}&carPlate=${carPlate}`
									});
								}
							}
						});
					} else if (hasCustomerUnFinishedOrder) {
						uni.showModal({
							title: '提示消息',
							content: '您有一个正在执行的订单，现在将跳转到司乘同显画面',
							showCancel: false,
							success: function(resp) {
								if (resp.confirm) {
									uni.navigateTo({
										url: '../move/move?orderId=' + result.unFinishedOrder
									});
								}
							}
						});
					}else if(orderInfo){
						//有未支付的订单,跳转到订单支付界面
						uni.showModal({
							title: '提示消息',
							content: '您有一个未支付的订单，现在将跳转到支付界面',
							showCancel: false,
							success: function(resp) {
								if (resp.confirm) {
									uni.navigateTo({
										url: '../order/order?orderId=' + result.orderId
									});
								}
							}
						});
					} else{
						// //跳转到创建新订单界面
						// uni.navigateTo({
						// 	url: '../create_order/create_order'
						// });
						if (location != null) {
							let place = location.name;
							let latitude = location.latitude;
							let longitude = location.longitude;
							if (that.flag == 'from') {
								that.from.address = place;
								that.from.latitude = latitude;
								that.from.longitude = longitude;
							} else {
								that.to.address = place;
								that.to.latitude = latitude;
								that.to.longitude = longitude;
								//跳转到创建订单页面
								uni.setStorageSync('from', that.from);
								uni.setStorageSync('to', that.to);
								
								uni.navigateTo({
									url: '../create_order/create_order'
								});
							}
						}
					}
				},
				false
			);
		}
	},

	onShow() {
		
		let that = this;
		// console.log("key值是:", that.tencent.map.key);
		
		that.map = uni.createMapContext('map');
		
		let qqmapsdk = new QQMapWX({
			key: that.tencent.map.key
		});

		that.qqmapsdk = qqmapsdk;

		uni.$on('updateLocation', function(location) {
			if (that.flag != null) {
				return;
			}
			let latitude = location.latitude;
			let longitude = location.longitude;
			that.latitude = latitude;
			that.longitude = longitude;
			that.from.latitude = latitude;
			that.from.longitude = longitude;

			qqmapsdk.reverseGeocoder({
				location: {
					latitude: latitude,
					longitude: longitude
				},
				success: function(resp) {
					that.from.address = resp.result.address;
				},
				fail: function(error) {
				}
			});
		});
		
		let location = chooseLocation.getLocation();
		
		
		//查询乘客当前订单
		this.queryCustomOrderState(location)
	},

	onHide: function() {
		
		uni.$off('updateLocation');
		
		//清除地图选点的结果
		chooseLocation.setLocation(null);
		
	},

	onLoad: function() {
		
		let that = this;
		let windowHeight = uni.getSystemInfoSync().windowHeight;
		that.windowHeight = windowHeight;
		that.contentStyle = `height:${that.windowHeight}px;`;

		//初始化当前位置信息
		this.initLocation();
	},

	onUnload: function() {
		//清除地图选点的结果
		chooseLocation.setLocation(null);
	}
};
</script>

<style lang="less">
@import url('workbench.less');
</style>
